//
//  RootChecker.m
//  gdemm
//
//  Created by 乔明超 on 16/10/27.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "RootChecker.h"
#import "ExamineManager.h"
#import "UserDefaultsUtil.h"
#import <sys/stat.h>
#import <dlfcn.h>
@interface RootChecker()
@property (nonatomic,assign) BOOL mIsRoot;
@end
@implementation RootChecker
static RootChecker *mInstance;
+ (instancetype)getInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mInstance = [[self alloc]init];
    });
    return mInstance;
    
}


//重写父类的方法
- (NSString *)getExamineDesc{
    return @"系统环境检测";
}

- (NSInteger)getRepairType{
    return RT_NOTSUPPORT;
}

- (BOOL)run{
    //如果是已ROOT的设备,则mIsRoot = false,为了和应用检查,wlan检查保持一致,即检查未通过均返回false.
    if ([self checkRootMethod]) {
        _mIsRoot = NO;
    }else{
        _mIsRoot = YES;
    }
    
    if (_mIsRoot) {
        [[ExamineManager getInstance].examineResult setValue:@1 forKey:EXAMINE_ROOT_STATUS_TAG];//测评安全
        [self reportExamineInfo:[NSString stringWithFormat:@"RootCheck&设备安全隐患&1"]];
    }else{
        [[ExamineManager getInstance].examineResult setValue:@"设备已越狱" forKey:EXAMINE_ROOT_STATUS_TAG];//测评不安全
        [self reportExamineInfo:[NSString stringWithFormat:@"RootCheck&设备安全隐患&0"]];
    }
    
    return _mIsRoot;
    
}

- (id)details{
    return nil;
}

- (NSInteger)getExamineContentCount{
    return 3;
}
- (BOOL)returnValue{
    return _mIsRoot;
}

- (void)reset{
    _mIsRoot = NO;
}
- (BOOL)checkRootMethod{
    
    [self reportExamineInfo:[NSString stringWithFormat:@"RootCheck&检查是否越狱&0"]];
    [NSThread sleepForTimeInterval:0.2];
    return ([self isJailBreak1]|| [self isJailBreak2]||[self isJailBreak3]||[self isJailBreak4]||[self isJailBreak5]);
}


//判断是否含有相关文件
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

const char* jailbreak_tool_pathes[] = {
    "/Applications/Cydia.app",
    "/Library/MobileSubstrate/MobileSubstrate.dylib",
    "/bin/bash",
    "/usr/sbin/sshd",
    "/etc/apt"
};
//查看你设备上是否多了越狱的目录
- (BOOL)isJailBreak1
{
    for (int i=0; i<ARRAY_SIZE(jailbreak_tool_pathes); i++) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:jailbreak_tool_pathes[i]]]) {
            return YES;
        }
    }
    return NO;
}
//判断cydia的URL scheme
- (BOOL)isJailBreak2
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) {
        return YES;
    }
    return NO;
}
//读取环境变量
char * printEnv(void)
{
    char  *env = getenv("DYLD_INSERT_LIBRARIES");
    return env;
}

- (BOOL)isJailBreak3
{
    if (printEnv()) {
        return YES;
    }
    return NO;
}
//使用stat方法来判定cydia是否存在
#define CYDIA_APP_PATH                "/Applications/Cydia.app"
int checkInject()
{
    int ret ;
    Dl_info dylib_info;
    int (*func_stat)(const char *, struct stat *) = stat;
    if ((ret = dladdr(func_stat, &dylib_info))) {
        return 0;
    }
    return 1;
}

int checkCydia()
{
    // first ,check whether library is inject
    struct stat stat_info;
    
    if (!checkInject()) {
        if (0 == stat(CYDIA_APP_PATH, &stat_info)) {
            return 1;
        }
    } else {
        return 1;
    }
    return 0;
}

- (BOOL)isJailBreak4
{
    if (checkCydia()) {
        return YES;
    }
    return NO;
    
}

//读取系统所有应用的名称，这个是利用不越狱的机器没有这个权限来判定的。
#define USER_APP_PATH                 @"/User/Applications/"
- (BOOL)isJailBreak5
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:USER_APP_PATH]) {
        
        return YES;
    }
    return NO;
}


- (BOOL)isRoot{
     return ([self isJailBreak1]|| [self isJailBreak2]||[self isJailBreak3]||[self isJailBreak4]||[self isJailBreak5]);
}
@end
